#include <iostream>
using namespace std;

class Solution {
public:
    int totalFruit(vector<int>& fruits) {
        int left = 0;
        int right = 0;
        int n = fruits.size();
        int len = 0;
        int arr[100001] = { 0 };
        int k = 0;
        while (right < n)
        {
            if (arr[fruits[right]] == 0)
            {
                k++;
            }
            arr[fruits[right++]]++;
            while (k > 2)
            {
                arr[fruits[left]]--;
                if (arr[fruits[left]] == 0)
                {
                    k--;
                }
                left++;
            }
            len = max(len, right - left);
        }
        return len;
    }
};

class Solution {
public:
    vector<int> findAnagrams(string s, string p) {
        vector<int> ret;
        int arr1[26] = { 0 };
        int arr2[26] = { 0 };
        int n1 = s.size();
        int n2 = p.size();
        for (int i = 0; i < n2; i++)
        {
            arr2[p[i] - 'a']++;
        }
        int left = 0;
        int right = 0;
        int coust = 0;
        if (coust == n2)
        {
            ret.push_back(left);
        }
        while (right < n1)
        {
            arr1[s[right] - 'a']++;
            if (arr1[s[right] - 'a'] <= arr2[s[right] - 'a'])
            {
                coust++;
            }
            if (right - left + 1 > n2)
            {
                arr1[s[left] - 'a']--;
                if (arr1[s[left] - 'a'] < arr2[s[left] - 'a'])
                {
                    coust--;
                }
                left++;
            }
            if (coust == n2)
            {
                ret.push_back(left);
            }
            right++;
        }
        return ret;
    }
};